var hanziReg= /^(([\u4e00-\u9fa5]{2,8})|([a-zA-Z]{2,16}))$/;
var descsReg= /^[\u4e00-\u9fa5\da-zA-Z\-\_]+$/; 
var phoneReg= /^[1][0-9]{10}$/;

var $receiverName = $("#receiverName");
var $receiverPhone = $("#receiverPhone");
var $address = $("#address");

var receiverNameflag = false;//公共flag 判断是不是验证通过
var receiverPhoneflag = false;//公共flag 判断是不是验证通过
var addressflag = false;//公共flag 判断是不是验证通过

(function(){

	$("#saveBtn").on("click", submit);

	$("#new").on("click", function(){
		$("#id").val("");
		$('#addressForm')[0].reset();
	});
	
	//输入框事件
    $receiverName.on("onreadystatechange input blur", function(){
		var $this = $(this);
		var receiverName = $this.val();

		receiverNameflag = validateForm($this, receiverName, hanziReg, "请输入正确的收货人姓名");
    });

    //输入框事件
    $receiverPhone.on("onreadystatechange input blur", function(){
		var $this = $(this);
		var receiverPhone = $this.val();

		receiverPhoneflag = validateForm($this, receiverPhone, phoneReg, "请输入正确的收货人手机号");
    });

	//输入框事件
    $address.on("onreadystatechange input blur", function(){
		var $this = $(this);
		var address = $this.val();

		addressflag = validateFormAddress($this, address, descsReg, "请输入正确的详细收货地址");
    });
	
	//表单验证, 前面四个都是必传参数：当前输入框、当前值、正则、提示文字，最后一个参数是可选参数：查看明文密码
	function validateFormAddress(that, value, reg, txt, cleartext){
		var $parent = that.closest(".form-group");
		var $addon = $parent.find(".input-group-addon");
		var $msg = $parent.find(".js-line-alert");
		var $msgText = $parent.find(".js-alert-text");

		var flag = true;

		if(value.length === 0){
    		modifyWarning($msgText, txt);
    		showWarning($addon, that, $msg, cleartext);
    		flag = false;
		}
		else if(value.length > 255){
    		modifyWarning($msgText, txt);
    		showWarning($addon, that, $msg, cleartext);
    		flag = false;
		}
		else if(!reg.test(value)){
    		modifyWarning($msgText, txt);
    		showWarning($addon, that, $msg, cleartext);
    		flag = false;
		}
		else{
			hideWarning($addon, that, $msg, cleartext);
		}

		return flag;
    }
	
	
    function validateForm(that, value, reg, txt, cleartext){
		var $parent = that.closest(".form-group");
		var $addon = $parent.find(".input-group-addon");
		var $msg = $parent.find(".js-line-alert");
		var $msgText = $parent.find(".js-alert-text");

		var flag = true;

		if(value.length === 0){
    		modifyWarning($msgText, txt);
    		showWarning($addon, that, $msg, cleartext);
    		flag = false;
		}
		else if(!reg.test(value)){
    		modifyWarning($msgText, txt);
    		showWarning($addon, that, $msg, cleartext);
    		flag = false;
		}
		else{
			hideWarning($addon, that, $msg, cleartext);
		}

		return flag;
    }

    //输入框边框变红并显示提示文字
    function showWarning(addon, input, msg, cleartext){
        if(cleartext){
          cleartext.addClass("input-error border-error");
        }
        addon.addClass("border-error");
        input.addClass("input-error border-error");
        msg.show();
    }

    //输入框边框恢复原色并隐藏提示文字
    function hideWarning(addon, input, msg, cleartext){
        if(cleartext){
          cleartext.removeClass("input-error border-error");
        }
        addon.removeClass("border-error");
        input.removeClass("input-error border-error");
        msg.hide();
    }

    //修改提示文字信息
    function modifyWarning(element, txt){
        element.text(txt);
    }
	
	//表单登录提交
    function submit(){
    	var id = $("#id").val();

    	var buyerId = $.trim($("#buyerId").val());
		var receiverName = $.trim($("#receiverName").val());
		var receiverPhone = $.trim($("#receiverPhone").val());
		var address_picker = $.trim($("#province").val())+" "+$.trim($("#city").val())+" "+$.trim($("#area").val());
		var address = $.trim($("#address").val());
		var isDefault = 0;

		if ($("#isDefault").get(0).checked){
			isDefault=1;
		}
			
		$receiverName.blur();
    	$receiverPhone.blur();
    	$address.blur();

		if(receiverNameflag && receiverPhoneflag && addressflag){
			if(id==""){
				$.ajax({
	            	url: "addrCrt",
	            	type: "post",
	            	dataType: 'json',
	            	data : {
						buyerId:buyerId,
						receiverName:receiverName,
						receiverPhone:receiverPhone,
						address_picker:address_picker,
						address:address,
						isDefault:isDefault
	            	},
	            	success: function(data) {
                        layer.open({
                            content: '添加成功！',
                            scrollbar: false
                        });
	            		location.reload();
	            	},
	            	error: function(xhr, type, errorThrown) {
	            		//异常处理；
						alert("请稍后再试!");
	            	}
				});
			}else{
				$.ajax({
	            	url: "addrUpdate",
	            	type: "post",
	            	dataType: 'json',
	            	data : {
	            		id:id,
						buyerId:buyerId,
						receiverName:receiverName,
						receiverPhone:receiverPhone,
						address_picker:address_picker,
						address:address,
						isDefault:isDefault
	            	},
	            	success: function(data) {
                        layer.open({
                            content: '修改成功！',
                            scrollbar: false
                        });
	            		location.reload();
	            	},
	            	error: function(xhr, type, errorThrown) {
	            		//异常处理；
						alert("请稍后再试!");
	            	}
				});
			}
			
			
			
		}
	}

	//-------------操作按钮绑定
	operation();

	function operation(){
		$(".edit").on("click", function(){
			var _this = $(this);
			var data = _this.attr("data-full");
			var id = data.split("@")[0];
			var receiverName = data.split("@")[1];
			var receiverPhone = data.split("@")[2];
			var province = data.split("@")[3];
			var city = data.split("@")[4];
			var area = data.split("@")[5];
			var address = data.split("@")[6];
			var isDefault = data.split("@")[7];

			$("#id").val(id);
			$("#receiverName").val(receiverName);
			$("#receiverPhone").val(receiverPhone);
    		$("#province").val(province);
    		$("#city").val(city);
    		$("#area").val(area);
			$("#address").val(address);

			if(isDefault==1){
				$("#isDefault").prop("checked",true);//选择 
			}else{
				$("#isDefault").prop("checked",false);//选择 
			}
		});
		
		$(".del").on("click", function(){
			var _this = $(this);
			var addressId = _this.attr("data-id");

			if(confirm("确认删除吗?")){
				$.ajax({
	            	url: "addrDel",
	            	type: "post",
	            	dataType: 'json',
	            	data : {
						id:addressId
	            	},
	            	success: function(data) {
                        layer.open({
                            content: '删除成功！',
                            scrollbar: false
                        });
	            		location.reload();
	            	},
	            	error: function(xhr, type, errorThrown) {
	            		//异常处理；
						alert("请稍后再试!");
	            	}
				});
			}

		});
		
		$(".isDef").on("click", function(){
			var _this = $(this);
			var addressId = _this.attr("data-id");

			$.ajax({
            	url: "setDefault",
            	type: "post",
            	dataType: 'json',
            	data : {
					id:addressId
            	},
            	success: function(data) {
                    layer.open({
                        content: '设置成功！',
                        scrollbar: false
                    });
            		location.reload();
            	},
            	error: function(xhr, type, errorThrown) {
            		//异常处理；
					alert("请稍后再试!");
            	}
			});
		});
	}
})();